package com.heima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;
    /**
     * 发送消息的方法
     * @return
     */
    @GetMapping("/send")
    public String sendMessage(){
        for (int i = 0; i <5 ; i++) {
            if(i%2==0){
                kafkaTemplate.send("kafka-stream-producer","hello kafka");
            }else{
                kafkaTemplate.send("kafka-stream-producer","hello itcast");
            }
        }
        return "发送消息成功";
    }
}
